package cn.player.utils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.LinkedList;
import java.util.List;

import com.coremedia.iso.IsoFile;
import com.coremedia.iso.boxes.Container;
import com.coremedia.iso.boxes.TrackBox;
import com.googlecode.mp4parser.FileDataSourceImpl;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Mp4TrackImpl;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;
import com.googlecode.mp4parser.authoring.tracks.AppendTrack;


public class VideoUtil {
	

/*	public static boolean mergeFiles(String speratedDirPath, String targetFileName) {
		double[] matrix = new double[] { 0.0, 1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
		File videoSourceDirFile = new File(speratedDirPath);
		String[] videoList = videoSourceDirFile.list();
		List<Track> videoTracks = new LinkedList<Track>();
		List<Track> audioTracks = new LinkedList<Track>();
		for (String file : videoList) {
			Log.d("zl", "source files" + speratedDirPath + File.separator + file);
			try {
				FileChannel fc = new FileInputStream(speratedDirPath + File.separator + file).getChannel();
				Movie movie = MovieCreator.build(fc);
				for (Track t : movie.getTracks()) {
					if (t.getHandler().equals("soun")) {
						audioTracks.add(t);
					}
					if (t.getHandler().equals("vide")) {
						videoTracks.add(t);
					}
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}finally{
				//TODO
			}
		}

		Movie result = new Movie();

		try {
			if (audioTracks.size() > 0) {
				result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
			}
			if (videoTracks.size() > 0) {
				result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
			}
			IsoFile out = new DefaultMp4Builder().build(result);
			FileChannel fc = new RandomAccessFile(String.format(speratedDirPath + File.separator + targetFileName), "rw").getChannel();
			Log.d("zl", "target file:" + targetFileName);
			TrackBox tb = out.getMovieBox().getBoxes(TrackBox.class).get(1);

			TrackHeaderBox tkhd = tb.getTrackHeaderBox();
			double[] b = tb.getTrackHeaderBox().getMatrix();
			tkhd.setMatrix(matrix);

			fc.position(0);
			out.getBox(fc);
			fc.close();
			
			for (String file : videoList) {
				File TBRFile = new File(speratedDirPath + File.separator + file);
				TBRFile.delete();
			}
			boolean a = videoSourceDirFile.delete();
			Log.d("zl", "try to delete dir:" + a);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

		return true;
	}*/
	
	public static void appendVideo(String speratedDirPath, String targetFileName) throws IOException {
		File videoSourceDirFile = new File(speratedDirPath);
		String[] videoList = videoSourceDirFile.list();
		Movie[] inMovies = new Movie[videoList.length];
		int index = 0;
		for (String video : videoList) {
			FileDataSourceImpl fdsl = new FileDataSourceImpl(speratedDirPath + File.separator + video);
			IsoFile isoFile = new IsoFile(fdsl);
	        Movie m = new Movie();
	        List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(TrackBox.class);
	        for (TrackBox trackBox : trackBoxes) {
	            m.addTrack(new Mp4TrackImpl(trackBox));
	        }
	        m.setMatrix(isoFile.getMovieBox().getMovieHeaderBox().getMatrix());
			
//			inMovies[index] = MovieCreator.build(speratedDirPath + File.separator + video);
			inMovies[index] = m;
			isoFile.close();
			index++;
		}
		List<Track> videoTracks = new LinkedList<Track>();
		List<Track> audioTracks = new LinkedList<Track>();
		for (Movie m : inMovies) {
			for (Track t : m.getTracks()) {
				if (t.getHandler().equals("soun")) {
					audioTracks.add(t);
				}
				if (t.getHandler().equals("vide")) {
					videoTracks.add(t);
				}
			}
		}

		Movie result = new Movie();

		if (audioTracks.size() > 0) {
			result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
		}
		if (videoTracks.size() > 0) {
			result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
		}
		
		Container out = new DefaultMp4Builder().build(result);
		RandomAccessFile raFile = new RandomAccessFile(String.format(speratedDirPath + File.separator + targetFileName), "rw");
		FileChannel fc = raFile.getChannel();
		out.writeContainer(fc);
		raFile.close();
		fc.close();
	}

	public static void appendVideo(String[] videos, String desitiFile) throws IOException {
		Movie[] inMovies = new Movie[videos.length];
		int index = 0;
		for (String video : videos) {
			inMovies[index] = MovieCreator.build(video);
			index++;
		}
		List<Track> videoTracks = new LinkedList<Track>();
		List<Track> audioTracks = new LinkedList<Track>();
		for (Movie m : inMovies) {
			for (Track t : m.getTracks()) {
				if (t.getHandler().equals("soun")) {
					audioTracks.add(t);
				}
				if (t.getHandler().equals("vide")) {
					videoTracks.add(t);
				}
			}
		}

		Movie result = new Movie();

		if (audioTracks.size() > 0) {
			result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
		}
		if (videoTracks.size() > 0) {
			result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
		}
		Container out = new DefaultMp4Builder().build(result);
//		FileChannel fc = new RandomAccessFile(String.format("D:\\Mp4\\append2.mp4"), "rw").getChannel();
		RandomAccessFile raFile = new RandomAccessFile(String.format(desitiFile), "rw");
		FileChannel fc = raFile.getChannel();
		out.writeContainer(fc);
		raFile.close();
		fc.close();
	}

}
